﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.UI.WebControls;

namespace QuanLyVanBanBUS
{
    public class PhongBUS
    {
        public void themPhong(Phong pDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            storeDB.Phongs.InsertOnSubmit(pDTO);

            storeDB.SubmitChanges();
        }

        public void suaPhong(Phong pDTO, string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.Phongs.Where(q => q.MaPhong == maPhong).First();
            query.TenPhong = pDTO.TenPhong;
            query.DienThoaiNoiBo = pDTO.DienThoaiNoiBo;
            query.DienThoaiNgoai = pDTO.DienThoaiNgoai;
            query.Fax = pDTO.Fax;

            storeDB.SubmitChanges();
        }

        public void xoaPhong(GridView gridView)
        {
            //Lay MaPhong duoc chon tu CheckBox
            string temp = string.Empty;

            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox _checkBox = (CheckBox)(gridView.Rows[i].Cells[0].FindControl("chkMaPhong"));

                if (_checkBox.Checked == true)
                {
                    string maPhongCanXoa = gridView.DataKeys[i].Value.ToString();

                    temp += maPhongCanXoa + ",";
                }
            }

            //Kiem tra co check vao CheckBox khong?
            if (temp.Length - 1 > 0)
            {
                temp = temp.Substring(0, temp.Length - 1);
            }
            else
            {
                //Response.Write("<script>alert('Xin vui lòng chọn Sản phẩm cần xóa')</script>");

                return;
            }

            //Xoa
            QLVBDataContext storeDB = new QLVBDataContext();

            //Them cac MaPhong duoc chon vao mang
            string[] m_temp = new string[] { temp };

            while (temp.Length > 0)
            {
                //Lay maTemp de lam dieu kien so sanh. Lay tu sau lay toi cua Mang
                string maTemp = temp.Substring(temp.LastIndexOf(",") + 1, temp.Length - (temp.LastIndexOf(",") + 1));

                var query = storeDB.Phongs.Where(q => q.MaPhong == maTemp).First();

                storeDB.Phongs.DeleteOnSubmit(query);

                storeDB.SubmitChanges();

                //Kiem tra dieu kien co con trong vong lap While khong?
                if ((temp.Length - (maTemp.Length + 1)) > 0)
                {
                    temp = temp.Substring(0, temp.Length - (maTemp.Length + 1));
                }
                else
                    break;
            }

            gridView.DataSource = layDanhSachPhong();
            gridView.DataBind();
        }

        public IList<Phong> layDanhSachPhong()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var lstPhong = storeDB.Phongs.ToList();

            return lstPhong;
        }

        public Boolean kiemtraTonTaiMaPhong(string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.Phongs.Where(q => q.MaPhong == maPhong).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public Boolean kiemtraTonTaiTenPhong(string tenPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.Phongs.Where(q => q.TenPhong == tenPhong).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public string layMaPhong(string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var _maPhong = storeDB.Phongs.Where(m => m.MaPhong == maPhong).Select(m => m.MaPhong).First();

            return _maPhong;
        }

        public string layTenPhong(string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var tenPhong = storeDB.Phongs.Where(m => m.MaPhong == maPhong).Select(m => m.TenPhong).First();

            return tenPhong;
        }

        public string layDienThoaiNoiBo(string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var dienThoaiNoiBo = storeDB.Phongs.Where(m => m.MaPhong == maPhong).Select(m => m.DienThoaiNoiBo).First();

            return dienThoaiNoiBo;
        }

        public string layDienThoaiNgoai(string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var dienThoaiNgoai = storeDB.Phongs.Where(m => m.MaPhong == maPhong).Select(m => m.DienThoaiNgoai).First();

            return dienThoaiNgoai;
        }

        public string layFax(string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var fax = storeDB.Phongs.Where(m => m.MaPhong == maPhong).Select(m => m.Fax).First();

            return fax;
        }

        public void themItemVaoDropDownList(DropDownList dropDownList)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.Phongs.ToList();
            foreach (var q in query)
            {
                var maPhong = q.MaPhong;
                var tenPhong = q.TenPhong;

                ListItem item = new ListItem();
                item.Text = tenPhong;
                item.Value = maPhong;

                dropDownList.Items.Add(item);
            }
        }

        public void sapxepPhong(DropDownList dropDownList, string maPhong)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var _maPhong = storeDB.Phongs.Where(m => m.MaPhong == maPhong).Select(m => m.MaPhong).First();

            dropDownList.SelectedValue = _maPhong.ToString();
        }

        public string layMaPhongTheoMaNguoiDung(string maNguoiDung)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var maPhong = storeDB.NguoiDungs.Where(m => m.MaNguoiDung == maNguoiDung).Select(m => m.MaPhong).First();

            return maPhong;
        }

        /*DOAN CODE NAY CHAY RAT TOT
        public void layDanhSachPhongLenTreeView(TreeView treeView)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            //
            TreeNode treeNode = new TreeNode("BIDV CAMAU");
            treeView.Nodes.Add(treeNode);

            //
            var query = storeDB.Phongs.ToList();
            foreach (var q in query)
            {
                //
                TreeNode phongNode = new TreeNode();

                //
                var _query = (from n in storeDB.NguoiDungs
                              join p in storeDB.Phongs on n.MaPhong equals p.MaPhong
                              where n.MaPhong == q.MaPhong
                              select n).ToList();
                foreach (var _q in _query)
                {
                    TreeNode tenNDNode = new TreeNode();
                    tenNDNode.Text = _q.TenNguoiDung;
                    phongNode.ChildNodes.Add(tenNDNode);
                }

                //
                phongNode.Text = q.MaPhong;
                treeNode.ChildNodes.Add(phongNode);
            }
        }*/

        //public string layMaPhongTheoMaNguoiDungMaVanBan(string maNguoiDung, string maVanBan)
        //{
        //    QLVBDataContext storeDB = new QLVBDataContext();

        //    var query = (from vb in storeDB.VanBans
        //                join nd in storeDB.NguoiDungs on vb.MaNguoiSoanThao equals nd.MaNguoiDung
        //                join p in storeDB.Phongs on nd.MaPhong equals p.MaPhong
        //                where vb.MaNguoiSoanThao == maNguoiDung && vb.MaVanBan == maVanBan
        //                select p.MaPhong).First();

        //    return query;
        //}

        public String layMaNguoiDungLanhDaoPhongCuaVanBanChuaDuyetTheoSoVanBan(string soVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            VanBanBUS vbBUS = new VanBanBUS();
            string maVanBan = vbBUS.layMaVanBanTheoSoVanBan(soVanBan);

            var query = (from nn in storeDB.NoiNhans
                         where nn.MaVanBan == maVanBan && nn.BiXoa == 0 && nn.KiemSoat == 0
                         select nn.MaNguoiNhan).First();

            return query;
        }

        public String layMaPhongCuaVanBanChuaDuyetTheoSoVanBan(string soVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            string maNguoiDungLanhDaoPhong = layMaNguoiDungLanhDaoPhongCuaVanBanChuaDuyetTheoSoVanBan(soVanBan);
            PhongBUS pBUS = new PhongBUS();
            string maPhong = pBUS.layMaPhongTheoMaNguoiDung(maNguoiDungLanhDaoPhong);
            
            VanBanBUS vbBUS = new VanBanBUS();
            string maVanBan = vbBUS.layMaVanBanTheoSoVanBan(soVanBan);

            var query = (from nn in storeDB.NoiNhans
                         join nd in storeDB.NguoiDungs on nn.MaNguoiNhan equals nd.MaNguoiDung
                         join p in storeDB.Phongs on nd.MaPhong equals p.MaPhong
                         where nn.MaVanBan == maVanBan && nn.BiXoa == 0 && nn.KiemSoat == 0 && nd.MaPhong == maPhong
                         select p.MaPhong).First();

            return query;
        }

        /*public IList<Phong> layMaPhongCuaVanBanChuaDuyetTheoSoVanBan(string soVanBan)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            string maNguoiDungLanhDaoPhong = layMaNguoiDungLanhDaoPhongCuaVanBanChuaDuyetTheoSoVanBan(soVanBan);
            PhongBUS pBUS = new PhongBUS();
            string maPhong = pBUS.layMaPhongTheoMaNguoiDung(maNguoiDungLanhDaoPhong);

            VanBanBUS vbBUS = new VanBanBUS();
            string maVanBan = vbBUS.layMaVanBanTheoSoVanBan(soVanBan);

            var query = (from nn in storeDB.NoiNhans
                         join nd in storeDB.NguoiDungs on nn.MaNguoiNhan equals nd.MaNguoiDung
                         join p in storeDB.Phongs on nd.MaPhong equals p.MaPhong
                         where nn.MaVanBan == maVanBan && nn.BiXoa == 0 && nn.KiemSoat == 0 && nd.MaPhong == maPhong
                         select p).ToList();

            return query;
        }*/
    }
}
